# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname='libffi'
pkgname=("${_realname}-git" "${_realname}-devel-git")
pkgver=v3.2.1.r283.gba2757a
pkgrel=2
pkgdesc="Portable, high level programming interface to various calling conventions"
arch=('i686' 'x86_64')
url="https://sourceware.org/libffi/"
groups=('libraries')
license=('MIT')
depends=()
checkdepends=('dejagnu')
#options=('!strip' 'debug')
install=libffi.install
source=("libffi"::"git+https://github.com/atgreen/libffi.git"
        config.sub
        config.guess
        0001-msys2ize.patch)
sha256sums=('SKIP'
            '6ef568e79ee95214ecb8b187bf14f168bd0889575e922e5e866c455186cfb063'
            '5744cab7fda627ef53dbd0d8cb1fa08b30d3702e01a621e028c5a12deee7c21c'
            '03af8e3eccb37048aa59a866bcd5f0b5b156ee28a0f59de1e7013a1d21ba4a18')

pkgver() {
  cd ${_realname}
  git describe --long | sed 's/\([^-]*-g\)/r\1/;s/-/./g'
}

prepare() {
  cd ${_realname}
  cp ${srcdir}/config.sub .
  cp ${srcdir}/config.guess .
  git am ${srcdir}/0001-msys2ize.patch

  autoreconf -fi
}

build() {
  cd ${_realname}
  # Perform the build outside of srcdir so that
  # srcdir is not simplified to '.' as relative
  # paths in debug information are not ideal.
  [ -d build ] && rm -rf build
  mkdir build
  cd build
  local -a extra_config
  if check_option "debug" "y"; then
    extra_config+=("--enable-debug")
  fi
  ${srcdir}/${_realname}/configure \
    --prefix=/usr \
    --srcdir=${srcdir}/${_realname} \
    --build=${CHOST} \
    --enable-static \
    --enable-shared \
    "${extra_config[@]}"
  make
  make DESTDIR="${srcdir}/dest" install
}

check() {
  cd ${_realname}/build
  make check
}

package_libffi-git() {
  provides=("libffi=${pkgver}")
  conflicts=("libffi")

  mkdir -p $pkgdir/usr/bin
  cp -f ${srcdir}/dest/usr/bin/*.dll ${pkgdir}/usr/bin/
  cp -rf ${srcdir}/dest/usr/share ${pkgdir}/usr/
  install -Dm644 ${srcdir}/${_realname}/LICENSE "${pkgdir}"/usr/share/licenses/${_realname}/LICENSE
}

package_libffi-devel-git() {
  pkgdesc="Libffi headers and libraries"
  groups=('development')
  if check_option "debug" "y"; then
    options=('staticlibs' '!strip' 'debug')
  else
    options=('staticlibs' '!strip' 'debug')
  fi
  depends=("libffi=${pkgver}")
  provides=("libffi-devel=${pkgver}")
  conflicts=("libffi-devel")

  mkdir -p ${pkgdir}/usr
  #cp -rf ${srcdir}/dest/usr/include ${pkgdir}/usr/
  cp -rf ${srcdir}/dest/usr/lib ${pkgdir}/usr/
}
